--[[ -------------------------------------------------------
Implements the MUSHclient MiniWindow functionality.

$URL:$

Copyright (C) 2009 Daesilme (Daniel W. Drake)

This file is part of mushclient-lua-objects.

mushclient-lua-objects is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software
Foundation, either version 2 of the License, or (at your
option) any later version.

mushclient-lua-objects is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public
License along with mushclient-lua-objects.  If not, see
<http://www.gnu.org/licenses/>.
------------------------------------------------------- ]]--

require("class");
require("MUSHclient.MiniWindow.Flags");
require("MUSHclient.MiniWindow.Info");
require("MUSHclient.MiniWindow.Position");

class "MiniWindow";

local MiniWindowClass = MiniWindow:classtable();

------------------------------------------------------------
-- PUBLIC CLASS PROPERTIES
------------------------------------------------------------

------------------------------------------------------------
-- PUBLIC CLASS METHODS
------------------------------------------------------------

------------------------------------------------------------
-- PUBLIC OBJECT PROPERTIES
------------------------------------------------------------

-- Returns the Original Left coordinate for this MiniWindow.
function MiniWindow:ORIG_LEFT()
	return Info:get(self.name, Info.ORIG.LEFT);
end   -- MiniWindow:ORIG_LEFT()

-- Returns the Original Top coordinate for this MiniWindow.
function MiniWindow:ORIG_TOP()
	return Info:get(self.name, Info.ORIG.TOP);
end   -- MiniWindow:ORIG_TOP()

-- Returns the Width of the MiniWindow.
function MiniWindow:WIDTH()
	return Info:get(self.name, Info.SIZE.WIDTH);
end   -- MiniWindow:WIDTH()

-- Returns the Height of the MiniWindow.
function MiniWindow:HEIGHT()
	return Info:get(self.name, Info.SIZE.HEIGHT);
end   -- MiniWindow:HEIGHT()

-- Returns if the MiniWindow is Visible.
function MiniWindow:VISIBLE()
	return Info:get(self.name, Info.VISIBLE);
end   -- MiniWindow:VISIBLE()

-- Returns if the MiniWindow is Hidden.
function MiniWindow:HIDDEN()
	return Info:get(self.name, Info.HIDDEN);
end   -- MiniWindow:HIDDEN()

-- Returns the Position of the MiniWindow.
function MiniWindow:POSITION()
	return Info:get(self.name, Info.POSITION);
end   -- MiniWindow:POSITION()

-- Returns the Flags of the MiniWindow.
function MiniWindow:FLAGS()
	return Info:get(self.name, Info.FLAGS);
end   -- MiniWindow:FLAGS()

-- Returns the Top coordinate of the MiniWindow.
function MiniWindow:TOP()
	return Info:get(self.name, Info.COORDS.TOP);
end   -- MiniWindow:TOP()

-- Returns the Left coordinate of the MiniWindow.
function MiniWindow:LEFT()
	return Info:get(self.name, Info.COORDS.LEFT);
end   -- MiniWindow:LEFT()

-- Returns the Bottom coordinate of the MiniWindow.
function MiniWindow:BOTTOM()
	return Info:get(self.name, Info.COORDS.BOTTOM);
end   -- MiniWindow:BOTTOM()

-- Returns the Right coordinate of the MiniWindow.
function MiniWindow:RIGHT()
	return Info:get(self.name, Info.COORDS.RIGHT);
end   -- MiniWindow:RIGHT()

------------------------------------------------------------
-- PUBLIC OBJECT METHODS
------------------------------------------------------------

function MiniWindow:toString()
	return
		string.format(
			"ORIG_LEFT  :  %d\n"				..
			"ORIG_TOP   :  %d\n"				..
			"SIZE       :  %d x %d\n"			..
			"VISIBLE    :  %s\n"				..
			"HIDDEN     :  %s\n"				..
			"POSITION   :  %s\n"				..
			"FLAGS      :  %s\n"				..
			"COORDS     :  %d, %d, %d, %d",
			self:ORIG_LEFT(),
			self:ORIG_TOP(),
			self:WIDTH(), self:HEIGHT(),
			tostring(self:VISIBLE()),
			tostring(self:HIDDEN()),
			Position:toString(self:POSITION()),
			Flags:toString(self:FLAGS()),
			self:TOP(), self:LEFT(), self:BOTTOM(), self:RIGHT()
		);

end   -- MiniWindow:toString()


-- Construct the MiniWindow object.
-- @name : The name of the MiniWindow
-- @title : The title bar text  (if nil, then @name)
-- @zorder : Used to position MiniWindows on top of each other  (if nil, then 50)
function MiniWindow:initialize(name, title, zorder)

	-- Check Parameters, and correct optional ones
	if(name == nil) then
		error("MiniWindow name cannot be nil");
	end

	if(title == nil) then
		title	=	name;
	end

	if((zorder == nil) or (tonumber(zorder) == nil)) then
		zorder = 50;
	end

	self.baseName	=	name;
	self.zorder		=	zorder;
	self.name		=	self:formatWindowName(name, zorder);

	self:create(
		0, 0, 0, 0,
		Position.TOP_LEFT,
		Flags.NONE,
		ColourNameToRGB("lightgray"),
		false
	);

end   -- MiniWindow:initialize(name, title, zorder)

-- Delete the MiniWindow
function MiniWindow:delete()
	check(WindowDelete(self.name));
end   -- MiniWindow:delete()

-- Change the visibility of the MiniWindow
function MiniWindow:show(state)
	check(WindowShow(self.name, state));
end

-- Set the Size of the MiniWindow
function MiniWindow:size(width, height)
end   -- MiniWindow:size(width, height)

------------------------------------------------------------
-- PRIVATE CLASS PROPERTIES
------------------------------------------------------------

------------------------------------------------------------
-- PRIVATE CLASS METHODS
------------------------------------------------------------

------------------------------------------------------------
-- PRIVATE OBJECT PROPERTIES
------------------------------------------------------------

------------------------------------------------------------
-- PRIVATE OBJECT METHODS
------------------------------------------------------------

-- Create the MUSHclient MiniWindow
-- TODO : IMPLEMENT
function MiniWindow:create(left, top, width, height, pos, flags, bgcolor, visible)
	WindowCreate(
		self.name,
		left, top, width, height,
		pos, flags,
		bgcolor
	);

	WindowShow(
		self.name,
		visible
	);

end   -- MiniWindow:create()

-- Returns a formatted window name
function MiniWindow:formatWindowName(baseName, zorder)
	return string.format("%02d-%s", zorder, baseName);
end   -- MiniWindow:formatWindowName(baseName, zorder)
